package com.zhang.controller;

import com.alibaba.fastjson.JSON;
import com.zhang.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class MyTest {

    @RequestMapping("/test")
    public String test() {

        return "null";
    }

    @RequestMapping("/t2")
    public void test2(String name, HttpServletResponse response) {
        System.out.println("a1:param==>" + name);
        if ("zhangbo".equals(name)) {
            try {
                response.getWriter().print("true");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                response.getWriter().print("false");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/a2")
    public Object a2() {
        List<User> list = new ArrayList<>();

        //添加数据
        list.add(new User("张波", 18, "男"));
        list.add(new User("李四", 15, "女"));
        list.add(new User("王五", 33, "男"));

        return JSON.toJSON(list);
    }

    @RequestMapping("/a3")
    public String a3(String name, String pwd) {
        String msg = null;
        if (name != null) {
            if ("admin".equals(name)) {
                msg = "ok";
            } else {
                msg = "用户名有误";
            }
        }
        if (pwd != null) {
            if ("123456".equals(pwd)) {
                msg = "ok";
            } else {
                msg = "密码有误";
            }
        }

        return msg;

    }
}
